package javase.homework;

import java.util.Map;

/**
 * **2. 实现一个 MapSum 类，支持两个方法，insert 和 sum：**
 * **MapSum() 初始化 MapSum 对象**
 * **void insert(String key, int val) 插入 key-val 键值对，字符串表示键 key ，整数表示值 val 。如果键 key 已经存在，那么原来的键值对将被替代成新的键值对。**
 * **int sum(string prefix) 返回所有以该前缀 prefix 开头的键 key 的值的总和。**
 */
class SingleLinkedListMapSum {
    public static void main(String[] args) {
        MapDemo m1 = new MapDemo("apple",3);
        MapDemo m2 = new MapDemo("ap",0);
        MapDemo m3 = new MapDemo("app",2);
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.insert(m1);
        singleLinkedList.insert(m2);
        singleLinkedList.insert(m3);
        singleLinkedList.select();

        MapDemo m4 = new MapDemo("app",5);
        System.out.println();
        singleLinkedList.insert(m4);
        singleLinkedList.select();
    }
}

class SingleLinkedList{
    //头结点
    private MapDemo head = new MapDemo("",0);

    //修改数据 根据key 修改value
    public void update(MapDemo mapDemo){
        MapDemo temp = head;
        while (true) {
            if (temp.next == null) {
                //数据不存在 进行添加n
                insert(mapDemo);
                break;
            }
            //如果找到相同key 的对象，将新的数值赋值给原来的对象
            if (temp.next.getKey().equals(mapDemo.getKey())) {
                temp = temp.next;
                temp.setValue(mapDemo.getValue());
                break;
            }
            temp = temp.next;
        }
    }

    //判断插入的元素是否存在
    public boolean selectIsEmpty(MapDemo mapDemo){
        if (head.next == null)
            return false;
        MapDemo temp = head;
        while (true){
            if (temp.next == null)
                return false;
            if (temp.next.getKey().equals(mapDemo.getKey()))
                return true;
            temp = temp.next;
        }
    }

    //添加结点
    public void insert(MapDemo mapDemo){
        MapDemo temp = head;
        while (true) {
            if (temp.next == null)
                break;
            //判断内部有没有重复key
            temp = temp.next;
        }
        if (selectIsEmpty(mapDemo)){
            //如果找到对应key
            update(mapDemo);
            return;
        }
        temp.next = mapDemo;
    }

    //遍历结点
    public void select(){
        MapDemo temp = head;
        while (true){
            if (temp.next == null)
                break;
            temp = temp.next;
            System.out.println(temp);
        }
    }
}

class MapDemo{
    private String key;
    private Integer value;
    MapDemo next;

    public MapDemo(String key, Integer value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "MapDemo{" +
                "key='" + key + '\'' +
                ", value=" + value +
                '}';
    }
}
